/*
 * 作者: 侯晓宇
 * 时间: 2017-02-04
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {

	require("jquery");
	require("blur");
	var CacheUtils = require('app/utils/CacheUtils');
	var c = require('app/utils/CommonUtils');
	var UserInfoUtils = require('app/utils/UserInfoUtils');
	var EventUtils = require('app/utils/EventUtils');
	var APIUtils = require('app/utils/APIUtils');
	var param = api.pageParam;

	var appInfo = require('app/AppInfo');

	var action = require("./action/IndexMeFrmAction");

	var user = UserInfoUtils.getLoginedUser();
	var headimgurl = "../image/user_avatar_default.jpg";

	if (!c.isNullStr(user.userheadimgurl)) {
		headimgurl = c.handleUrl(user.userheadimgurl);
	}

	
	var head_h = $("#head-content").height();


	if(c.isNullStr(user.usertype)){
		if(c.isNullStr(user.usertel)){
			$("#usertype").html('未授权');
		}else{
			$("#usertype").html('等待审核');
		}	
	}else if(user.usertype == '1'){
		$("#usertype").html('已授权');
	}else if(user.usertype == '9'){
		$("#usertype").html('管理员权限');
		$("#admin").show();
	}

	$("#username").html(user.username);
	$("#userheadimgurl").attr("src", headimgurl);

	//从本地数据库中更新页面用户信息：包括用户名与头像，如果头像为空
	//判断头像字符串是否为空 c.isNullStr()
	action.getUserInfo();

	//从服务器获取关注信息：关注数、粉丝数
	action.followInfo();

	$("#username").click(function() {
//		var name = "RoomUserInfoFrm";
//		api.openFrame({
//			name: name,
//			url: './' + name + '.html',
//			rect: {
//				x: 0,
//				y: 0,
//				w: APIUtils.winW(),
//				h: APIUtils.winH()
//			},
//			bgColor: 'rgba(0,0,0,0.6)',
//			bounces: false
//		});
	});

	$("#card").click(function() {
		var name = "IndexMeMyCardWin";
		api.openWin({
			name: name,
			url: './' + name + '.html',
			rect: {
				x: 0,
				y: 0,
				w: APIUtils.winW(),
				h: APIUtils.winH()
			},
			bounces: false
		});
	});

	$("#myLive").click(function() {
		action.openMyRoomFrm();
	});
	$("#setting").click(function() {
		action.openSettingWin();
	});
	$("#marker").click(function() {
		action.openMyLikeFrm();
	});

	$("#editInfo").click(function() {
		action.openEditInfo();
	});
	
	$("#inviteFriend").click(function(){
		var contacts = api.require('contacts');
		contacts.select(function(ret, err) {
		    if (ret.status) {
		    	api.confirm({
		    		title:'提示框',
		    		msg:'是否要向这个号码发送一条信息邀请？',
		    		buttons:['确定','取消']
                },function(ret1,err){
                	var index = ret1.buttonIndex;
                	if(index==1){
                	//index=1 “确定”按钮被触发
//              		console.log(index);
	                	var array = ret.phones;
//				        console.log("姓名：" + ret.fullName);
//				        console.log("长度：" + array.length);
//		
				        for(var i=0;i<array.length;i++){
				        	var key = [];
				        	for(var p1 in array[i]){
				        		//if(array[i].hasOwnProperty(p1))
				        		key.push(p1);	
				        	}
				 			console.log("key的长度："+key.length);
				        	console.log("标签："+key[0]);
				        	var ph = eval('array[i].'+key[0]);
				       		console.log("手机号："+ ph);
				        }
				        api.alert({msg:ret1.text});  
                	}              	
                });
		    } else {
		        alert(JSON.stringify(err));
		    }
		});
	});
	
	$("#cache_clear").click(function() {
		api.clearCache({}, function(ret, err) {
			api.toast({
				msg: '清理存储空间完成'
			});
			$("#cache_size").text("0 MB");
		});
		CacheUtils.deleteImageCache();
	});

	$("#account").click(function() {

		api.openWin({
			name : 'CompleteUserInfoWin',
			url : './CompleteUserInfoWin.html',
			bounces : false,
			reload : true
		});
	});
	
	$("#about").click(function(){
		api.openWin({
			name : 'AboutWin',
			url : './AboutWin.html',
			bounces : false,
			reload : true
		});
	});

	$("#aboutus").click(function(){
		api.openWin({
			name : 'AboutUsWin',
			url : './AboutUsWin.html',
			bounces : false,
			reload : true
		});
	});

	$("#logout").click(function() {
		UserInfoUtils.logout();
		EventUtils.send('root', 'reset');
	});
	
	api.getCacheSize(function(ret) {
		var cache_size = parseInt(ret.size / 1024 / 1024 * 100) / 100 + ' MB';
		$("#cache_size").text(cache_size);
	});

	$("#version").html(appInfo.version);


	$("#admin").click(function(){
		api.openWin({
	        name: 'ManageWin',
	        url: './ManageWin.html',
	        pageParam: param
        });	
	});
	return;
});